package threaduse;

/**
 * @author DCSGO
 * @version 1.0
 * 线程使用案例-多线程执行
 * 请编写一个程序，创建两个线程，一个线程每隔1秒输出"hello,world"
 * 输出10次，退出，一个线程每隔1秒输出"hi"，输出5次退出
 * 要求实现 Runnable 接口
 */
public class Thread03 {
    public static void main(String[] args) {
        Hello hello = new Hello();
        Thread threadHello = new Thread(hello);

        Hi hi = new Hi();
        Thread threadHi = new Thread(hi);

        threadHello.start();

        threadHi.start();
        //Hello Thread: Thread-0
        //hello,world 1
        //Hi Thread: Thread-1
        //hi 1
        //hi 2
        //hello,world 2
        //hello,world 3
        //hi 3
        //hello,world 4
        //hi 4
        //hi 5
        //hello,world 5
        //hello,world 6
        //hello,world 7
        //hello,world 8
        //hello,world 9
        //hello,world 10
        //
        //进程已结束，退出代码为 0
    }
}

class Hello implements Runnable {
    @Override
    public void run() {
        System.out.println("Hello Thread: " + Thread.currentThread().getName());
        for (int i = 1; i <= 10; i++) {
            System.out.println("hello,world " + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class Hi implements Runnable {
    @Override
    public void run() {
        System.out.println("Hi Thread: " + Thread.currentThread().getName());
        for (int i = 1; i <= 5; i++) {
            System.out.println("hi " + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
